﻿using System;
using System.Configuration;

namespace NetworkSupervisor.Helper
{

    public static class ConfigHelper
    {

        #region ProviderNameType enum

        public enum ProviderNameType
        {

            Sqlserver,

            Access,

            Sqlite

        }

        #endregion

        /// <summary>

        /// 获取connectionStrings值

        /// </summary>

        /// <param name="name"></param>

        /// <returns></returns>

        public static ConnectionStringSettings GetConnectionStrings(string name)
        {

            foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
            {

                if (item.Name == name)
                {

                    return ConfigurationManager.ConnectionStrings[name];

                }

            }

            return null;

        }

        /// <summary>

        /// 设置connectionStrings值

        /// </summary>

        /// <param name="name"></param>

        /// <param name="connectionString"></param>

        /// <param name="providerNameType"></param>

        public static void SetConnectionStrings(string name, string connectionString, ProviderNameType providerNameType)
        {

            bool isExist = false;

            foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
            {

                if (name == item.Name)
                {

                    isExist = true;

                    break;

                }

            }

            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (isExist)
            {

                configuration.ConnectionStrings.ConnectionStrings.Remove(name);

            }

            string providerName;

            switch (providerNameType)
            {

                case ProviderNameType.Sqlite:

                    providerName = "System.Data.SQLite";

                    break;

                case ProviderNameType.Sqlserver:

                    providerName = "System.Data.SqlClient";

                    break;

                case ProviderNameType.Access:

                    providerName = "Microsoft.Jet.OLEDB.4.0";

                    break;

                default:

                    throw new ArgumentOutOfRangeException("providerNameType");

            }

            var connectionStringSettings = new ConnectionStringSettings(name, connectionString, providerName);

            configuration.ConnectionStrings.ConnectionStrings.Add(connectionStringSettings);

            configuration.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("connectionStrings");

        }

        /// <summary>

        /// 获取appSettings值

        /// </summary>

        /// <param name="key">key</param>

        /// <returns></returns>

        public static string GetAppSettings(string key)
        {

            foreach (string item in ConfigurationManager.AppSettings)
            {

                if (key == item)
                {

                    return ConfigurationManager.AppSettings[key];

                }

            }

            return null;

        }

        /// <summary>

        /// 设置appSettings值

        /// </summary>

        /// <param name="key">key</param>

        /// <param name="value">value</param>

        public static void SetAppSettings(string key, string value)
        {

            bool isExist = false;

            foreach (string item in ConfigurationManager.AppSettings)
            {

                if (key == item)
                {

                    isExist = true;

                    break;

                }

            }

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (isExist)
            {

                config.AppSettings.Settings.Remove(key);

            }

            config.AppSettings.Settings.Add(key, value);

            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("appSettings");

        }

    }

}